package com.yhyvr.api.tool;

import com.alibaba.fastjson.JSON;
import com.lz.kit.LogKit;
import com.yhyvr.pojo.req.api.ApiReqData;
import com.yhyvr.pojo.resp.api.ApiRespData;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * Created by zhangqin on 2016/11/28.
 */
@Service
public class ToolReturnJson {
        private static final String contentType = "application/json; charset=UTF-8";
        private static final String contentTypeForIE = "text/html; charset=UTF-8";
        public void renderJson(ApiRespData<?> apiRespData, ApiReqData<?> apiReqData){
            apiRespData.getHead().setOtherParameters(apiReqData.getHead().getOtherParameters());
            String domain =apiReqData.getHead().getCrossDomain();
            HttpServletResponse response = apiReqData.getHead().getHttpServletResponse();
            PrintWriter writer = null;
            try {
                response.setHeader("Pragma", "no-cache");	// HTTP/1.0 caches might not implement Cache-Control and might only implement Pragma: no-cache
                response.setHeader("Cache-Control", "no-cache");
                response.setDateHeader("Expires", 0);
                // response.setHeader("P3P","CP=CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR");
                response.setContentType(contentType);
                writer = response.getWriter();
                if(StringUtils.isNotBlank(domain)){
                    writer.write(domain+"("+ JSON.toJSONString(apiRespData)+")");
                }else{
                    writer.write(JSON.toJSONString(apiRespData));
                }

                writer.flush();
            } catch (IOException e) {
                LogKit.error(e.getMessage(),e);
            }
            finally {
                if (writer != null)
                    writer.close();
            }
        }
        public void renderJson(HttpServletResponse response, ApiRespData<?> apiRespData){

            PrintWriter writer = null;
            try {
                response.setHeader("Pragma", "no-cache");	// HTTP/1.0 caches might not implement Cache-Control and might only implement Pragma: no-cache
                response.setHeader("Cache-Control", "no-cache");
                response.setDateHeader("Expires", 0);

                response.setContentType(contentType);
                writer = response.getWriter();
                writer.write(JSON.toJSONString(apiRespData));
                writer.flush();
            } catch (IOException e) {
                LogKit.error(e.getMessage(),e);
            }
            finally {
                if (writer != null)
                    writer.close();
            }
        }

        public void renderJson(HttpServletResponse response, ApiRespData<?> apiRespData,String domain){

            PrintWriter writer = null;
            try {
                response.setHeader("Pragma", "no-cache");	// HTTP/1.0 caches might not implement Cache-Control and might only implement Pragma: no-cache
                response.setHeader("Cache-Control", "no-cache");
                response.setDateHeader("Expires", 0);

                response.setContentType(contentType);
                writer = response.getWriter();
                if(StringUtils.isNotBlank(domain)){
                    writer.write(domain+"("+JSON.toJSONString(apiRespData)+")");
                }else{
                    writer.write(JSON.toJSONString(apiRespData));
                }

                writer.flush();
            } catch (IOException e) {
                LogKit.error(e.getMessage(),e);
            }
            finally {
                if (writer != null)
                    writer.close();
            }
        }
    }

